#########################
#########################
#Bot Analysis
#########################
#########################

rm(list = ls())
setwd()
# Packages
pacman::p_load(rtweet,
               ggplot2,
               purrrlyr,
               dplyr,
               readxl,
               writexl,
               broom,
               tidyverse,
               dotwhisker,
               lubridate,
               stargazer,
               ggpubr, 
               jtools,
               rpart,
               caret,
               reshape2,
               ipred,
               RColorBrewer,
               rpart.plot,
               MASS,
               randomForest,
               dplyr,
               car,
               vtable,
               effects,
               interactions
               
)

set.seed(0611)

#Import data
df <- readRDS("df.bots.RDS")

#Only include people who have at least one follower 
df <- df %>% filter(followers_count > 0)


#############################################
#############################################
#Rerun analysis for DF30 & DF80 in bot score
#############################################
#############################################

#DF 30 first -> filter
df30 <- df %>% filter(df$pred.bot <= .3)
#DF80 -> filter
df80 <- df %>% filter(df$pred.bot <= .8)

#################
# Poisson Models--No Interaction
#################

#Favorites

m.conspir.po.fav <- glm(favorite_count ~ conspir.po + followers_count + 
                          friends_count + 
                          verified + month , 
                        family = "quasipoisson",
                        data = df30)


m.conspir.collusion.fav <- glm(favorite_count ~ conspir.collusion + followers_count + 
                                 friends_count +
                                 verified + month  , 
                               family = "quasipoisson",
                               data = df30)

m.conspir.tusk.fav <- glm(favorite_count ~ conspir.tusk + followers_count + 
                            friends_count + 
                            verified + month , 
                          family = "quasipoisson",
                          data = df30)
#Retweets 
m.conspir.po.rt <- glm(retweet_count ~ conspir.po + followers_count + 
                         friends_count + 
                         verified + month, 
                       family = "quasipoisson",
                       data = df30)

m.conspir.collusion.rt <- glm(retweet_count ~ conspir.collusion + followers_count + 
                                friends_count + 
                                verified + month, 
                              family = "quasipoisson",
                              data = df30)
m.conspir.tusk.rt <- glm(retweet_count ~ conspir.tusk + followers_count + 
                           friends_count + 
                           verified + month, 
                         family = "quasipoisson",
                         data = df30)
stargazer(m.conspir.po.fav, m.conspir.tusk.fav, m.conspir.collusion.fav, 
          type = "text",
          header = FALSE,
          align = TRUE,
          label = "tab:bot.control.fav",
          #column.sep.width = "-15pt",
          title = "Tweet Content and Frequency of Favorites Models with Bot Controls",
          dep.var.caption = c("Number of Favorites"),
          model.names = FALSE,
          omit = c("month"), #so it's prettier
          covariate.labels = c("PO Consp.",
                               "Tusk Consp.",
                               "Russia-PO Consp.",
                               "N(Followers)",
                               "N(Friends)",
                               "Verified",
                               
                               "Constant"),
          notes.append = FALSE,
          notes = c("Standard Errors in parenthesis.", 
                    "*p $<$ 0.1; **p $<$ .05, ***p $<$ .01 (two-tailed tests)."))

stargazer(m.conspir.po.rt, m.conspir.tusk.rt, m.conspir.collusion.rt, 
          type = "text",
          header = FALSE,
          align = TRUE,
          label = "tab:bot.30.rt",
          #column.sep.width = "-15pt",
          title = "Tweet Content and Frequency of Retweets Models with Bot Controls",
          dep.var.caption = c("Number of Favorites"),
          model.names = FALSE,
          omit = c("month"), #so it's prettier
          covariate.labels = c("PO Consp.",
                               "Tusk Consp.",
                               
                               "Russia-PO Consp.",
                               "N(Followers)",
                               "N(Friends)",
                               "Verified",
                               
                               "Constant"),
          notes.append = FALSE,
          notes = c("Standard Errors in parenthesis.", 
                    "*p $<$ 0.1; **p $<$ .05, ***p $<$ .01 (two-tailed tests)."))

#################
#Quasi Poisson Models--PiS Interaction
#################

#Favorites

m.conspir.po.fav <- glm(favorite_count ~ conspir.po*officials + followers_count + 
                          friends_count + 
                          verified + month, 
                        family = "quasipoisson",
                        data = df30)



m.conspir.collusion.fav <- glm(favorite_count ~ conspir.collusion*officials + followers_count + 
                                 friends_count +
                                 verified + month, 
                               family = "quasipoisson",
                               data = df30)

m.conspir.tusk.fav <- glm(favorite_count ~ conspir.tusk*officials + followers_count + 
                            friends_count + 
                            verified + month, 
                          family = "quasipoisson",
                          data = df30)
#Retweets 
m.conspir.po.rt <- glm(retweet_count ~ conspir.po*officials + followers_count + 
                         friends_count + 
                         verified + month, 
                       family = "quasipoisson",
                       data = df30)
m.conspir.col.rt <- glm(retweet_count ~ conspir.collusion*officials + followers_count + 
                          friends_count + 
                          verified + month, 
                        family = "quasipoisson",
                        data = df30)

m.conspir.tusk.rt <- glm(retweet_count ~ conspir.tusk*officials + followers_count + 
                           friends_count + 
                           verified + month, 
                         family = "quasipoisson",
                         data = df30)
stargazer(m.conspir.po.fav, m.conspir.tusk.fav, m.conspir.collusion.fav,
          type = "text",
          header = FALSE,
          align = TRUE,
          label = "tab:bot.30.fav.pis",
          #column.sep.width = "-15pt",
          title = "Tweet Content, PiS Officials, and Frequency of Favorites Models with Bot 30 Data",
          dep.var.caption = c("Number of Favorites"),
          model.names = FALSE,
          omit = c("month"), #so it's prettier
          covariate.labels = c("PO Consp.",
                               "Tusk Consp.",
                               
                               "Russia-PO Consp.",
                               "*PiS Official",
                               "N(Followers)",
                               "N(Friends)",
                               "Verified",
                               "PO Consp.*PiS Official",
                               "Tusk Consp.*PiS Official",
                             
                               "Russia-PO Consp.*PiS Official",
                               "Constant"),
          notes.append = FALSE,
          notes = c("Standard Errors in parenthesis.", 
                    "*p $<$ 0.1; **p $<$ .05, ***p $<$ .01 (two-tailed tests)."))
stargazer(m.conspir.po.rt, m.conspir.tusk.rt,  m.conspir.col.rt,
          type = "text",
          header = FALSE,
          align = TRUE,
          label = "tab:bot.30.rt.pis",
          #column.sep.width = "-15pt",
          title = "Tweet Content, PiS Officials, and Frequency of Retweets Models with Bot 30 Data",
          dep.var.caption = c("Number of Retweets"),
          model.names = FALSE,
          omit = c("month"), #so it's prettier
          covariate.labels = c("PO Consp.",
                               "Tusk Consp.",
                             
                               "Russia-PO Consp.",
                               "PiS Official",
                               "N(Followers)",
                               "N(Friends)",
                               "Verified",
                               "PO Consp.*PiS Official",
                               "Tusk Consp.*PiS Official",
                              
                               "Russia-PO Consp.*PiS Official",
                               "Constant"),
          notes.append = FALSE,
          notes = c("Standard Errors in parenthesis.", 
                    "*p $<$ 0.1; **p $<$ .05, ***p $<$ .01 (two-tailed tests)."))

#############################################
#War Models
#############################################


#For Collusion Conspiracy
mod.col.fav <- glm(favorite_count ~ conspir.collusion*War + followers_count + 
                     friends_count + 
                     verified, 
                   family = "quasipoisson",
                   data = df30)


mod.col.rt <- glm(retweet_count ~ conspir.collusion*War + followers_count + 
                    friends_count + 
                    verified, 
                  family = "quasipoisson",
                  data = df30)


#For Tusk Conspiracy 
mod.tusk.fav <- glm(favorite_count ~ conspir.tusk*War + followers_count + 
                      friends_count + 
                      verified, 
                    family = "quasipoisson",
                    data = df30)


mod.tusk.rt <- glm(retweet_count ~ conspir.tusk*War + followers_count + 
                     friends_count + 
                     verified, 
                   family = "quasipoisson",
                   data = df30)
##FOr PO
#For Tusk Conspiracy 
mod.po.fav <- glm(favorite_count ~ conspir.po*War + followers_count + 
                    friends_count + 
                    verified , 
                  family = "quasipoisson",
                  data = df30)


mod.po.rt <- glm(retweet_count ~ conspir.po*War + followers_count + 
                   friends_count + 
                   verified, 
                 family = "quasipoisson",
                 data = df30)

stargazer(mod.po.fav, mod.tusk.fav, mod.col.fav,
  #mod.po.rt, mod.tusk.rt,  mod.col.rt,
  type = "text",
  header = FALSE,
  align = TRUE,
  label = "tab:bot.30.rt.war",
  #column.sep.width = "-15pt",
  title = "Tweet Content, War, and Frequency of Likes Models with Bot 30 Data",
  dep.var.caption = c("Number of Likes"),
  model.names = FALSE,
  #omit = c("month"), #so it's prettier
  covariate.labels = c("PO Consp.",
                       "Tusk Consp.",
                     
                       "Russia-PO Consp.",
                       "War",
                       "N(Followers)",
                       "N(Friends)",
                       "Verified",
                       "PO Consp.*War",
                       "Tusk Consp.*War",
                       
                       "Russia-PO Consp.*War",
                       "Constant"),
  notes.append = FALSE,
  notes = c("Standard Errors in parenthesis.", 
            "*p $<$ 0.1; **p $<$ .05, ***p $<$ .01 (two-tailed tests)."))

stargazer(#mod.po.fav, mod.tusk.fav, mod.rus.fav, mod.col.fav,
          mod.po.rt, mod.tusk.rt,  mod.col.rt,
          type = "text",
          header = FALSE,
          align = TRUE,
          label = "tab:bot.30.rt.war",
          #column.sep.width = "-15pt",
          title = "Tweet Content, War, and Frequency of Retweets Models with Bot 30 Data",
          dep.var.caption = c("Number of Retweets"),
          model.names = FALSE,
          #omit = c("month"), #so it's prettier
          covariate.labels = c("PO Consp.",
                               "Tusk Consp.",
                               
                               "Russia-PO Consp.",
                               "War",
                               "N(Followers)",
                               "N(Friends)",
                               "Verified",
                               "PO Consp.*War",
                               "Tusk Consp.*War",
                               
                               "Russia-PO Consp.*War",
                               "Constant"),
          notes.append = FALSE,
          notes = c("Standard Errors in parenthesis.", 
                    "*p $<$ 0.1; **p $<$ .05, ***p $<$ .01 (two-tailed tests)."))
######################
#TRIPLE INTERACTIONS
######################
##########
#Favorites
##########

m.conspir.po.fav <- glm(favorite_count ~ conspir.po*War*officials  + verified + month +
                          followers_count + 
                          friends_count,
                        family = "quasipoisson",
                        data = df30)
m.conspir.tusk.fav <- glm(favorite_count ~ conspir.tusk*War*officials + verified + month +
                            followers_count + 
                            friends_count, 
                          family = "quasipoisson",
                          data = df30)

m.conspir.collusion.fav <- glm(favorite_count ~ conspir.collusion*War*officials + verified + month +
                                 followers_count + 
                                 friends_count, 
                               family = "quasipoisson",
                               data = df30)

##########
#Retweets
##########

m.conspir.po.rt <- glm(retweet_count ~ conspir.po*War*officials + verified + month +
                         followers_count + 
                         friends_count, 
                       family = "quasipoisson",
                       data = df30)
m.conspir.tusk.rt <- glm(retweet_count ~ conspir.tusk*War*officials + verified + month +
                           followers_count + 
                           friends_count, 
                         family = "quasipoisson",
                         data = df30)

m.conspir.collusion.rt<- glm(retweet_count ~ conspir.collusion*War*officials +  verified + month +
                               followers_count + 
                               friends_count,  
                             family = "quasipoisson",
                             data = df30)

stargazer(m.conspir.po.fav, m.conspir.tusk.fav, m.conspir.collusion.fav, 
          #m.conspir.po.rt, m.conspir.tusk.rt, m.conspir.collusion.rt, m.conspir.rus.rt,
          type = "text",
          header = FALSE,
          align = TRUE,
          label = "tab:triple.inter.qp.bot30.fav",
          title = "Quasi-Poisson Bot 30 Models Favorites",
          dep.var.labels = c("Number of Favorites" #,
                             #"Number of Retweets"
          ),
          model.names = FALSE,
          omit = c("month"), #so it's prettier
          covariate.labels = c("PO Consp.",
                               "Tusk Consp.",
                               "Russia and PO Consp.",
                               
                               "War",
                               "PiS Officials",
                               "Verified",
                               "N(Followers)",
                               "N(Friends)",
                               "PO Consp.*War",
                               "PO Consp.*PiS Official",
                               "Tusk Consp.*War",
                               "Tusk Consp.*PiS Official",
                               "Collusion Consp.*War",
                               "Collusion Consp.*PiS Official",
                              
                               "War*PiS Official",
                               "PO Consp.*War*Official",
                               "Tusk Consp.*War*Official",
                               "Collusion Consp.*War*Official",
                             
                               "Constant"),
          notes.append = FALSE,
          notes = c("Standard Errors in parenthesis.", 
                    "*p $<$ 0.1; **p $<$ .05, ***p $<$ .01 (two-tailed tests)."))

stargazer(m.conspir.po.rt, m.conspir.tusk.rt, m.conspir.collusion.rt,
          type = "text",
          header = FALSE,
          align = TRUE,
          label = "tab:triple.inter.qp.bot30.rt",
          title = "Quasi-Poisson Bot 30 Models Retweets",
          dep.var.labels = c("Number of Retweets"
          ),
          model.names = FALSE,
          omit = c("month"), #so it's prettier
          covariate.labels = c("PO Consp.",
                               "Tusk Consp.",
                               "Russia and PO Consp.",
                               
                               "War",
                               "PiS Officials",
                               "Verified",
                               "N(Followers)",
                               "N(Friends)",
                               "PO Consp.*War",
                               "PO Consp.*PiS Official",
                               "Tusk Consp.*War",
                               "Tusk Consp.*PiS Official",
                               "Collusion Consp.*War",
                               "Collusion Consp.*PiS Official",
                               
                               "War*PiS Official",
                               "PO Consp.*War*Official",
                               "Tusk Consp.*War*Official",
                               "Collusion Consp.*War*Official",
                               
                               "Constant"),
          notes.append = FALSE,
          notes = c("Standard Errors in parenthesis.", 
                    "*p $<$ 0.1; **p $<$ .05, ***p $<$ .01 (two-tailed tests)."))
